package com.dns.customers.dao.impl;

import com.dns.customers.dao.ExportDao;
import com.util.DataSourceUtil;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author :  yulei
 * @data :  2018/7/27 15:11
 * @Version :  1.0
 **/

@Repository
public class ExportDaoImpl  extends DataSourceUtil  implements ExportDao {

    @Override
    public List<Map<String, Object>> queryOrderDetailByOrderId(String orderId) {

        StringBuffer sql = new StringBuffer();
        sql.append(" SELECT ORDER_ID,DOMAIN_NAME,TYPE,RES,");
        sql.append(" DATE_FORMAT(EXECU_TIME,'%Y-%m-%d %H:%i:%s') EXECU_TIME,");
        sql.append(" DATE_FORMAT(UPDATE_TIME,'%Y-%m-%d %H:%i:%s') UPDATE_TIME,");
        sql.append(" IF(B.AREA_CODE=-1,'全市',(SELECT AREA_NAME  ");
        sql.append("  FROM  TD_M_CHINA_AREA m   WHERE m.province_code = b.province_code AND M.AREA_CODE = B.AREA_CODE)) AREA_NAME ,");
        sql.append("(SELECT   AREA_NAME  FROM TD_M_CHINA_AREA N  WHERE N.AREA_CODE = B.PROVINCE_CODE ) PROVINCE_NAME ");
        sql.append(" FROM DNS_ORDER_DETAIL B WHERE ORDER_ID=:ORDER_ID ");

        Map<String,Object> param = new HashMap<String,Object> ();
        param.put("ORDER_ID",orderId);
        List<Map<String, Object>> result=  daoManager.queryForList(sql.toString(),param);

        return result;
    }

    /**
     * 根据客户id查询客户的所有根域名
     * @param custId 客户id
     * @return
     */
    @Override
    public List<String> queruRootDomainByCustId(String custId)  throws Exception{
        String sql = "select domain_name from dns_domain_info where domain_type='0' and cust_Id=:custId";
        MapSqlParameterSource param = new MapSqlParameterSource();
        param.addValue("custId",custId);
        List<Map<String,Object>> list = daoManager.queryForList(sql,param);
        if (list == null || list.size() <=0)
            return null;
        List<String> domainList = new ArrayList<>();
        for (Map<String,Object> domain : list){
            String domainName = (String)domain.get("domain_name");
            if (domainName != null){
                domainList.add(domainName);
            }
        }
        return domainList;
    }
}
